﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Model;
using VAdvantage.Controls;
using System.Windows.Data;

namespace VAdvantage.Grids
{
    public class VDataGridAccountColumn : DataGridBoundColumn, IDataGridColumn
    {

        #region Property

        public string DataPropertyName
        {
            get
            {
                return _columnName;
            }
            set
            {
                _columnName = value.ToUpper();
                SetBinding();
            }
        }

        private void SetBinding()
        {
            this.Binding = new Binding(_columnName);
            conveter = new AcctValueToTextConveter();
            if (_field != null)
            {
                conveter.DataSource = _field.GetLookup();
            }
            this.Binding.Converter = conveter;
        }

        public Controls.VDataGrid DataGridView
        {
            get;
            set;
        }

        public GridField Field
        {
            get
            {
                return _field;
            }
            set
            {
                _field = value;
                if (_field != null)
                {
                    editingControl = new VAccount(_field.GetColumnName(), _field.IsMandatory(false), _field.IsReadOnly(),
                                                    _field.IsEditable(false),  (MAccountLookup)_field.GetLookup(),_field.GetHeader());
                    editingControl.SetField(_field);

                    _lookup = _field.GetLookup();

                    _displayType = _field.GetDisplayType();
                    if (conveter != null)
                    {
                        conveter.DataSource = _field.GetLookup();
                    }

                }
            }
        }

        public void Dispose()
        {
            if (editingControl != null)
            {
                editingControl.Dispose();
            }
            editingControl = null;
            if (conveter != null)
            {
                conveter.Dispose();
            }
            conveter = null;
            _field = null;
            hostTextBlock = null;
        }

        public bool ReadOnly
        {

            get;
            set;

        }

        #endregion

        #region

        private GridField _field;
        private Lookup _lookup = null;

        private VAccount editingControl = null;
        private TextBlock hostTextBlock = null;
        private object currentCellValue = null;
        private int _displayType;

        private String _columnName;
        
        AcctValueToTextConveter conveter = null;


        #endregion

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            if (editingControl == null)
            {
                editingControl = new VAccount();
            }

            hostTextBlock = (TextBlock)cell.Content;

            editingControl.SetValueInDataGridColumn(null);

            currentCellValue = ((DataUtil.DataObject)dataItem).GetFieldValue(_columnName);

            return editingControl;
        }

        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            
            if (cell.Content is VAccount)
            {
                editingControl.VetoableChangeListener -= new EventHandler(editingControl_VetoableChangeListener);
                currentCellValue = ((VAccount)cell.Content).GetValue();
                _field.SetValue(currentCellValue);
                ((DataUtil.DataObject)dataItem).SetFieldValue(_columnName, currentCellValue);
                //editingControl.SetText("");

                return hostTextBlock;
            }

            TextBlock text = Utility.AEnv.GetDataGridElement();

            if (this.Binding != null)
            {
                text.SetBinding(TextBlock.TextProperty, this.Binding);
            }
            return text;
        }

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
           // if (editingEventArgs.OriginalSource is TextBlock)
            //{
                editingControl.EditingControlDataGridView = DataGridView;
                editingControl.SetValueInDataGridColumn(currentCellValue);
                editingControl.VetoableChangeListener += new EventHandler(editingControl_VetoableChangeListener);
            //}
            return editingElement;
        }

        void editingControl_VetoableChangeListener(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }
    }
}
